﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Gift.DAL.crm
{
    /// <summary>
    /// 数据访问类:t_DALRule
    /// </summary>
    public partial class t_DALRule
    {
        public t_DALRule()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("RuleID", "t_Rule");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int RuleID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_Rule");
            strSql.Append(" where RuleID=@RuleID ");
            SqlParameter[] parameters = {
					new SqlParameter("@RuleID", SqlDbType.Int,4)			};
            parameters[0].Value = RuleID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Gift.Model.crm.t_Rule model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_Rule(");
            strSql.Append("RuleID,Description,Name,ParentID,Level,Disable,Url)");
            strSql.Append(" values (");
            strSql.Append("@RuleID,@Description,@Name,@ParentID,@Level,@Disable,@Url)");
            SqlParameter[] parameters = {
					new SqlParameter("@RuleID", SqlDbType.Int,4),
					new SqlParameter("@Description", SqlDbType.VarChar,50),
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@ParentID", SqlDbType.Int,4),
					new SqlParameter("@Level", SqlDbType.Int,4),
					new SqlParameter("@Disable", SqlDbType.Bit,1),
					new SqlParameter("@Url", SqlDbType.VarChar,200)};
            parameters[0].Value = model.RuleID;
            parameters[1].Value = model.Description;
            parameters[2].Value = model.Name;
            parameters[3].Value = model.ParentID;
            parameters[4].Value = model.Level;
            parameters[5].Value = model.Disable;
            parameters[6].Value = model.Url;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gift.Model.crm.t_Rule model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_Rule set ");
            strSql.Append("Description=@Description,");
            strSql.Append("Name=@Name,");
            strSql.Append("ParentID=@ParentID,");
            strSql.Append("Level=@Level,");
            strSql.Append("Disable=@Disable,");
            strSql.Append("Url=@Url");
            strSql.Append(" where RuleID=@RuleID ");
            SqlParameter[] parameters = {
					new SqlParameter("@Description", SqlDbType.VarChar,50),
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@ParentID", SqlDbType.Int,4),
					new SqlParameter("@Level", SqlDbType.Int,4),
					new SqlParameter("@Disable", SqlDbType.Bit,1),
					new SqlParameter("@Url", SqlDbType.VarChar,200),
					new SqlParameter("@RuleID", SqlDbType.Int,4)};
            parameters[0].Value = model.Description;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.ParentID;
            parameters[3].Value = model.Level;
            parameters[4].Value = model.Disable;
            parameters[5].Value = model.Url;
            parameters[6].Value = model.RuleID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int RuleID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_Rule ");
            strSql.Append(" where RuleID=@RuleID ");
            SqlParameter[] parameters = {
					new SqlParameter("@RuleID", SqlDbType.Int,4)			};
            parameters[0].Value = RuleID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string RuleIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_Rule ");
            strSql.Append(" where RuleID in (" + RuleIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.crm.t_Rule GetModel(int RuleID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 RuleID,Description,Name,ParentID,Level,Disable,Url from t_Rule ");
            strSql.Append(" where RuleID=@RuleID ");
            SqlParameter[] parameters = {
					new SqlParameter("@RuleID", SqlDbType.Int,4)			};
            parameters[0].Value = RuleID;

            Gift.Model.crm.t_Rule model = new Gift.Model.crm.t_Rule();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["RuleID"] != null && ds.Tables[0].Rows[0]["RuleID"].ToString() != "")
                {
                    model.RuleID = int.Parse(ds.Tables[0].Rows[0]["RuleID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Description"] != null && ds.Tables[0].Rows[0]["Description"].ToString() != "")
                {
                    model.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Name"] != null && ds.Tables[0].Rows[0]["Name"].ToString() != "")
                {
                    model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ParentID"] != null && ds.Tables[0].Rows[0]["ParentID"].ToString() != "")
                {
                    model.ParentID = int.Parse(ds.Tables[0].Rows[0]["ParentID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Level"] != null && ds.Tables[0].Rows[0]["Level"].ToString() != "")
                {
                    model.Level = int.Parse(ds.Tables[0].Rows[0]["Level"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Disable"] != null && ds.Tables[0].Rows[0]["Disable"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["Disable"].ToString() == "1") || (ds.Tables[0].Rows[0]["Disable"].ToString().ToLower() == "true"))
                    {
                        model.Disable = true;
                    }
                    else
                    {
                        model.Disable = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["Url"] != null && ds.Tables[0].Rows[0]["Url"].ToString() != "")
                {
                    model.Url = ds.Tables[0].Rows[0]["Url"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RuleID,Description,Name,ParentID,Level,Disable,Url ");
            strSql.Append(" FROM t_Rule ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" RuleID,Description,Name,ParentID,Level,Disable,Url ");
            strSql.Append(" FROM t_Rule ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM t_Rule ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.RuleID desc");
            }
            strSql.Append(")AS Row, T.*  from t_Rule T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "t_Rule";
            parameters[1].Value = "RuleID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

